<template>
  <!-- 使用插槽  渲染数据 -->
  <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width:100%">
    <el-col>
      <span>{{ data.name }}</span>
    </el-col>
    <el-col :span="4">
      <el-row type="flex" justify="end">
        <!-- 两个内容 -->
        <el-col>{{ data.manager }}</el-col>
        <el-col>
          <!-- 下拉菜单 element -->
          <el-dropdown @command="handleCommand">
            <span>
              操作<i class="el-icon-arrow-down" />
            </span>
            <!-- 下拉菜单 -->
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="add">添加子部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="edit">编辑部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="del">删除部门</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { delDepartment } from '@/api/departments'
export default {
  props: {
    data: {
      type: Object,
      required: true
    },
    isRoot: {
      type: Boolean
    }
  },
  methods: {
    handleCommand(command) {
      if (command === 'add') {
        this.$emit('addDialog', this.data)
      }

      if (command === 'edit') {
        this.$emit('editDialog', this.data)
      }

      if (command === 'del') {
        this.$confirm('是否确认删除该部门？', '删除部门', {
          confirmButtonText: '别墨迹，赶紧删',
          cancelButtonText: '别回头',
          type: 'warning'
        }).then(() => {
          // 处理点击确认的情况
          delDepartment(this.data.id).then(res => {
            this.$emit('updateData')
            this.$message.success('删除成功')
          }).catch(() => {
            this.$message.error('删除失败')
          })
          // this.$message()
        }).catch(() => {
          // 处理取消的逻辑
        })
      }
    }
  }
}
</script>

<style>

</style>
